/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.server.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;

/**
 *
 * @author Administrator
 */
public class ToJsonUtil {

    static Logger logger = Logger.getLogger(ToJsonUtil.class.getName());

    public static String jsonUtil(
            String opid, String areaid,
            String userareaid, String sysid,
            String taskid, String sysuserid,
            String msgid, String name, String value) {

        JSONObject jSONObject = new JSONObject();
        jSONObject.put("opid", "020004");
        jSONObject.put("areaid", "320000");//先填死
        jSONObject.put("userareaid", "320000");//userareaid和destareaid一样
        jSONObject.put("sysid", "WXB");
        jSONObject.put("taskid", "020004");//taskid=opid
        jSONObject.put("sysuserid", "admin");//一定是admin
        jSONObject.put("msgid", "020004");
        JSONArray paramObject = new JSONArray();
        JSONObject nameJSON = new JSONObject();
        nameJSON.put("name", "url");
        nameJSON.put("value", "/fs/fhfs/v1/eAHk/20170412/http/HbG8iM/1671/258412/99287");
        paramObject.add(nameJSON);
        jSONObject.put("param", paramObject);
        String json = jSONObject.toJSONString();
        return json;
    }

    public static String string2Json(
            String opid, String areaid,
            String userareaid, String sysid,
            String taskid, String sysuserid,
            String msgid, String name, String value) {
        if (sysuserid.equals("") || areaid.equals("") || userareaid.equals("") || sysid.equals("") || name.equals("") || value.equals("") || opid.equals("") || msgid.equals("") || taskid.equals("")) {
            logger.info("parameter error");
            return "";
        } else {
            return "{\"sysuserid\":\"" + sysuserid + "\",\"areaid\":\"" + areaid + "\",\"userareaid\":\"" + userareaid + "\",\"sysid\":\"" + sysid + "\",\"param\":[{\"name\":\"" + name + "\",\"value\":\"" + value + "\"}],\"opid\":\"" + opid + "\",\"msgid\":\"" + msgid + "\",\"taskid\":\"" + taskid + "\"}";
        }
    }

    public static void main(String[] args) {
        String opid = "020004";
        String areaid = "320000";
        String userareaid = "320000";
        String sysid = "WXB";
        String taskid = "020004";
        String sysuserid = "admin";
        String msgid = "020004";
        String name = "url";
        String value = "/fs/fhfs/v1/eAHk/20170412/http/HbG8iM/1671/258412/99287";
        System.out.println(jsonUtil(opid, areaid, userareaid, sysid, taskid, sysuserid, msgid, name, value));
        System.out.println(string2Json(opid, areaid, userareaid, sysid, taskid, sysuserid, msgid, name, value));
//        JSONObject addGroupMonitorJson = JSONObject.parseObject(jsonUtil(opid, areaid, userareaid, sysid, taskid, sysuserid, msgid, name, value));
        String s = "{\"opid\": \"1334403000000000035\",\"areaid\": \"440300\",\"userareaid\": \"440300\",\"sysid\": \"WXB\",\"taskid\": \"1334403000000000035\",\"sysuserid\": \"admin\",\"param\": [{\"name\": \"userid\",\"value\": \"125454892\"},{\"name\": \"accountname\",\"value\": \"chilei107\"}]}";
//        String s="{\"src\":204,\"file_list\":[{\"name\":\"/20170822/204_20170822_9ed21132-42eb-4154-8192-24d18a6cfdb8.json\"},{\"name\":\"/20170822/204_20170822_9ed21132-42eb-4154-8192-24d18a6cfdb7.json\"}]}";
        JSONObject addGroupMonitorJson = JSONObject.parseObject(s);
        JSONArray param = addGroupMonitorJson.getJSONArray("param");
        Object removeobj = null;
        for (int i = 0; i < param.size(); i++) {
            logger.info(param.size());
            JSONObject obi = param.getJSONObject(i);
            if (obi.getString("name").equals("userid")) {
                String useridValue = obi.getString("value");
                logger.info(useridValue);
                removeobj = param.getJSONObject(i);
            }
            if (obi.getString("name").equals("accountname")) {
                String accountnameValue = obi.getString("value");
                logger.info(accountnameValue);
            }
        }

        //由于fh接口只能接收一个参数，选定accountname作为保留参数进行发送，将userid去掉
        boolean flag = param.remove(removeobj);
        logger.info("remove userid " + flag + ",and request send to fh is " + addGroupMonitorJson.toJSONString());

//        System.out.println(param.size()+""+param);
//        String ss = param.getJSONObject(0).getString("name");
//        System.out.println(ss);
//        System.out.println(topics_val);
    }
}
